<template>
  <div>
    <img
      src="../static/img/comment/comment-top.png"
      class="w100 comment-top"
    >
    <div class="bg-white">
      <div class="title border-bottom">
        评论
      </div>
      <div class="textarea-bg pr row space-between">
        <v-textarea
          v-model="commentData"
          :loading="loading"
          :rounded="false"
          :auto-grow="true"
          append-icon="mdi-pencil"
          rows="3"
          class="mx-2 pl5"
          placeholder="快来给美丽的照片评论一下吧"
        />
        <div class="comment-btn">
          <v-btn @click="addComment">
            发送
          </v-btn>
        </div>
      </div>
      <div
        v-for="(item,index) in list"
        :key="item.date"
      >
        <v-card
          v-if="index<maxCommentNum"
          max-width="344"
          class="mx-auto border-top"
        >
          <v-list-item>
            <v-list-item-avatar :color="item.avatar?item.avatar:'grey'">
              <img
                v-if="item.avatar"
                :src="item.avatar"
              >
            </v-list-item-avatar>
            <v-list-item-content>
              <v-list-item-title>
                {{ item.nickname ?item.nickname:' ' }}
                <div class="time fr">
                  {{ item.created_at }}
                </div>
              </v-list-item-title>
              <v-list-item-subtitle class="mt5">
                {{ item.content }}
              </v-list-item-subtitle>
            </v-list-item-content>
          </v-list-item>
        </v-card>
      </div>
      <div
        v-if="list.length>3&&maxCommentNum===3"
        class="more-bg tc"
        @click="maxCommentNum=999999"
      >
        显示更多评论
        <v-icon color="#25939A">
          mdi-chevron-down
        </v-icon>
      </div>
    </div>
  </div>
</template>

<script>
import http from '../assets/js/http'

export default {
  name: 'Comment',
  props: {
    id: {
      type: String,
      default: '1'
    },
    list: {
      type: Array,
      default: () => []
    }
  },
  data () {
    return {
      commentData: null,
      loading: false,
      maxCommentNum: 3
    }
  },
  methods: {
    addComment () {
      if (this.commentData.length > 0) {
        this.loading = true
        const tempCommentData = this.commentData
        http.post('/api/greengage/avatar/add_comment', {
          avatar: this.$store.state.userInfo.avatar,
          openid: this.$store.state.openid,
          unionid: this.$store.state.unionid,
          content: this.commentData,
          nickname: this.$store.state.userInfo.nickName,
          id: this.id
        }).then((res) => {
          this.loading = false
          if (res.code === 200) {
            this.list.push({
              content: tempCommentData,
              nickname: this.$store.state.userInfo.nickName,
              avatar: this.$store.state.userInfo.avatar,
              created_at: transformTime()
            })
            this.commentData = ''
          }
        })
      }
      function transformTime (timestamp = +new Date()) {
        if (timestamp) {
          const time = new Date(timestamp)
          const y = time.getFullYear() // getFullYear方法以四位数字返回年份
          const M = time.getMonth() + 1 // getMonth方法从 Date 对象返回月份 (0 ~ 11)，返回结果需要手动加一
          const d = time.getDate() // getDate方法从 Date 对象返回一个月中的某一天 (1 ~ 31)
          const h = time.getHours() // getHours方法返回 Date 对象的小时 (0 ~ 23)
          const m = time.getMinutes() // getMinutes方法返回 Date 对象的分钟 (0 ~ 59)
          const s = time.getSeconds() // getSeconds方法返回 Date 对象的秒数 (0 ~ 59)
          return y + '-' + addZero(M) + '-' + addZero(d) + ' ' + addZero(h) + ':' + addZero(m) + ':' + addZero(s)
        } else {
          return ''
        }
      }
      function addZero (num) {
        return num < 10 ? '0' + num : num
      }
    }
  }
}
</script>

<style scoped>

  .title:before{
    content: "";
    display: block;
    position: absolute;
    vertical-align: middle;
    width: 4px;
    height: 18px;
    background: #D5AF5E;
    border-radius: 3px;
    left: 10px;
    top: 20%;
    -webkit-transform: translateY(10%);
    transform: translateY(10%);
  }
  .title{
    text-indent: 20px;
  }
  .comment-top{
    vertical-align: bottom;
    margin-bottom: -2px;
    width: 101%;
  }
  .time{
    width: 135px;
    color: #939393;
    text-align: right;
    font-size: 14px;
  }
  .more-bg{
    font-size: 14px;
    color: #25939A;
    padding: 30px 0;
  }
  .v-card:last-child{
    box-shadow: none;
  }
  .textarea-bg{
    width: 92%;
    margin: 0 auto;
  }
</style>
